import { reactive } from "vue";
import { UserControllerService } from "../../../generated";
import ACCESS_ENUM from "@/access/accessEnum";

/**
 * 用户信息
 */
let loginUser = reactive({
  id: null as string | null,
  userName: null as string | null,
  userProfile: null as string | null,
  userRole: null as string | null,
  createTime: null as Date | null,
  updateTime: null as Date | null,
});

/**
 * 处理用户登录
 */
async function getLoginUser() {
  const res = await UserControllerService.getLoginUserUsingGet();
  if (res.code === 0) {
    loginUser.userName = res.data?.userName || null;
    loginUser.userRole = res.data?.userRole || null;
  } else {
    const user = {
      userName: loginUser.userName,
      userRole: ACCESS_ENUM.NOT_LOGIN,
    };
    loginUser = Object.assign(loginUser, user);
  }
}

export { loginUser, getLoginUser };
